package com.cazj.controller;

import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import org.activiti.engine.task.Task;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.cazj.common.util.ShiroUtils;
import com.cazj.common.vo.JsonResult;
import com.cazj.pojo.LeaveBill;
import com.cazj.service.WorkflowService;

@Controller
@RequestMapping("workflow")
public class WorkFlowController {
	
	@Autowired
	private WorkflowService workflowService;
	
	//用户新建请假申请时, 跳转至新增编辑页
	@RequestMapping("workflow_leave_new")
	public String workflow_leave_edit(Model model) {
		String username = ShiroUtils.getEmpName();
		model.addAttribute("username", username);
		return "cazj/workflow_leave_edit";
	}
	
	@RequestMapping("workflow_leave_save")
	@ResponseBody
	public JsonResult workflow_leave_save(LeaveBill leaveBill) {
		workflowService.saveLeaveBill(leaveBill);
		return new JsonResult("save ok");
	}
	
	//用户发起流程的页面
	@RequestMapping("workflow_prepared_start")
	//@ResponseBody
	public String workflow_prepared_start(Model model,String workflowType) {
		Map<String,Object> map=new LinkedHashMap<>();
		//查询请假单列表
		List<LeaveBill> leaveBillList=workflowService.findLeaveBillList();
		map.put("leaveBillList", leaveBillList);
		
		//return new JsonResult(map);
		model.addAttribute("workflowList", map);
		return "cazj/workflow_prepared_start";
	}
	
	//用户发起流程的页面中, 点到删除按妞
	@RequestMapping("workflow_leave_delete")
	@ResponseBody
	public JsonResult workflow_leave_delete(
			String workflowType,
			Integer workflowId) {
		
		switch (workflowType) {
		case "LeaveBill":
			workflowService.deleteLeaveBillById(workflowId);
			break;
		default:
			break;
		}
		
		return new JsonResult("delete ok");
	}
	
	//用户发起流程的页面中, 点到发起流程按妞
	@RequestMapping("workflow_leave_start")
	@ResponseBody
	public JsonResult workflow_leave_start(
			String workflowType,
			Integer workflowId) {
		
		switch (workflowType) {
		case "LeaveBill":
			workflowService.startLeaveBillProcess(workflowId);
			break;
		default:
			break;
		}
		
		return new JsonResult("start ok");
	}
	
	//<需我处理>的页面呈现, 需要查询的是acvitivi中,runtimeservice任务列表.
	@RequestMapping("workflow_handle_list")
	public String workflow_handle_list(Model model) {
		String Assignee=ShiroUtils.getEmpName();
		List<Map<String, Object>> TaskList = workflowService.findTaskListByName(Assignee);
		//System.out.println(TaskList);
		model.addAttribute("listTask", TaskList);
		return "cazj/workflow_handle_list";

	}
}
